package com.wiigeetest;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;

import org.wiigee.control.AndroidWiigee;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.ToggleButton;

public class TrainingScreen extends Activity
{
	public static final int TRAIN_BUTTON = 10;
	public static final int CLOSE_GESTURE_BUTTON = 11;
	public static final int RECOGNITION_BUTTON = 12;
	
	AndroidWiigee andgee;
	SensorManager sm;
	
	ToggleButton btnRecord;
	Button btnRegister;
	Button btnClear;
	Button btnInstructions;
	AlertDialog.Builder save;
	AlertDialog.Builder instructions;
	TextView txtCount;
	
	int count;
	int numGestures;
	
	public void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.training);
		
		andgee = new AndroidWiigee(getApplicationContext());
        andgee.setTrainButton(TRAIN_BUTTON);
        andgee.setCloseGestureButton(CLOSE_GESTURE_BUTTON);
        andgee.setRecognitionButton(RECOGNITION_BUTTON);
        
        sm = (SensorManager)getSystemService(SENSOR_SERVICE);
        sm.registerListener(andgee.getDevice(), sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_FASTEST);
        try {
			andgee.getDevice().setAccelerationEnabled(true);
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		count = 0;
		txtCount = (TextView)findViewById(R.id.txtCount);
		
		btnRecord = (ToggleButton)findViewById(R.id.btnRecord);
		btnRecord.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if (btnRecord.isChecked())
				{
					andgee.getDevice().fireButtonPressedEvent(TRAIN_BUTTON);
					btnRegister.setEnabled(false);
				}
				else
				{
					andgee.getDevice().fireButtonReleasedEvent(TRAIN_BUTTON);
					count++;
					txtCount.setText("Current gesture samples: " + count);
					
					if (count >= 6)
						btnRegister.setEnabled(true);
				}
				
			}
		});
		
		btnRegister = (Button)findViewById(R.id.btnRegister);
		btnRegister.setEnabled(false);
		btnRegister.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				btnRegister.setEnabled(false);
				
				save = new AlertDialog.Builder(TrainingScreen.this);
				final EditText filename = new EditText(TrainingScreen.this);
				save.setView(filename);
				save.setPositiveButton("OK", new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						String fn = filename.getText().toString().trim();
						try
						{
							String s = andgee.getDevice().saveGesture(numGestures);
							
							FileOutputStream fo = openFileOutput(fn, MODE_PRIVATE);
							OutputStreamWriter osw = new OutputStreamWriter(fo);
							osw.write(s);
							osw.flush();
							osw.close();
							count = 0;
							txtCount.setText("Current gesture samples: " + count);
							numGestures++;
							btnRegister.setEnabled(false);
						}
						catch (IOException e)
						{
							System.out.println("Error: Write to File!");
							e.printStackTrace();
						}
					}
				});
				save.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						btnRegister.setEnabled(true);
						dialog.cancel();
					}
				});
				
				andgee.getDevice().fireButtonPressedEvent(CLOSE_GESTURE_BUTTON);
				andgee.getDevice().fireButtonReleasedEvent(CLOSE_GESTURE_BUTTON);
				
				save.show();
			}
		});
		
		btnClear = (Button)findViewById(R.id.btnClear);
		btnClear.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				andgee.getDevice().fireButtonPressedEvent(CLOSE_GESTURE_BUTTON);
				andgee.getDevice().fireButtonReleasedEvent(CLOSE_GESTURE_BUTTON);
				count = 0;
				txtCount.setText("Current gesture samples: " + count);
			}
		});
		
		btnInstructions = (Button)findViewById(R.id.btnInstructions);
		instructions = new AlertDialog.Builder(this);
		instructions.setMessage("Instructions: Press Record to begin recording. Press it again to finish capture. Repeat this process at least 6 times (around 9 is optimal), then press Register to name and save the gesture. Press Clear at any time to start over.");
		instructions.setPositiveButton("OK", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				return;
			}
		});
		btnInstructions.setOnClickListener(new OnClickListener() {	
			@Override
			public void onClick(View v) {
				instructions.show();
			}
		});
		
		
	}
}
